home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / help.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  19KB  |  731 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: help.c%v 3.50.1.16 1993/08/27 05:04:42 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - help.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software is provided "as is" without express or implied warranty.
  21.  * 
  22.  *
  23.  * AUTHORS
  24.  * 
  25.  *   Original Software:
  26.  *     Thomas Williams,  Colin Kelley.
  27.  * 
  28.  *   Gnuplot 2.0 additions:
  29.  *       Russell Lang, Dave Kotz, John Campbell.
  30.  *
  31.  *   Gnuplot 3.0 additions:
  32.  *       Gershon Elber and many others.
  33.  * 
  34.  * There is a mailing list for gnuplot users. Note, however, that the
  35.  * newsgroup 
  36.  *     comp.graphics.gnuplot 
  37.  * is identical to the mailing list (they
  38.  * both carry the same set of messages). We prefer that you read the
  39.  * messages through that newsgroup, to subscribing to the mailing list.
  40.  * (If you can read that newsgroup, and are already on the mailing list,
  41.  * please send a message info-gnuplot-request@dartmouth.edu, asking to be
  42.  * removed from the mailing list.)
  43.  *
  44.  * The address for mailing to list members is
  45.  *       info-gnuplot@dartmouth.edu
  46.  * and for mailing administrative requests is 
  47.  *       info-gnuplot-request@dartmouth.edu
  48.  * The mailing list for bug reports is 
  49.  *       bug-gnuplot@dartmouth.edu
  50.  * The list of those interested in beta-test versions is
  51.  *       info-gnuplot-beta@dartmouth.edu
  52.  */
  53.  
  54. #include <stdio.h>
  55. #if defined (ATARI)
  56. #include "plot.h"
  57. #else
  58.  
  59. extern int strcmp();
  60. extern int      strlen();
  61. extern char *strcpy();
  62. extern char *strncpy();
  63. extern char *strcat();
  64. extern char *strncat();
  65. extern char *getenv();
  66. extern FILE *fopen();
  67. extern char *malloc();
  68. #endif
  69.  
  70. #include <errno.h>
  71. extern int errno;
  72.  
  73. extern int instring();
  74.  
  75. #define    SAME    0    /* for strcmp() */
  76.  
  77. #include "help.h"    /* values passed back */
  78.  
  79. #if defined(__EMX__) || defined(DJGPP) || defined(DOS386)
  80. #ifdef MSDOS
  81. #undef MSDOS    /* we have plenty of memory under __EMX__ or DJGPP */
  82. #endif
  83. #ifdef unix
  84. #undef unix    /* we are not unix */
  85. #endif
  86. #endif
  87.  
  88. #ifdef OS2
  89.   /* GCC defines unix, but no PAGER, so... */
  90. #ifdef unix
  91. #undef unix
  92. #endif
  93. #endif  /* OS2 */
  94.  
  95. /* help -- help subsystem that understands defined keywords
  96. **
  97. ** Looks for the desired keyword in the help file at runtime, so you
  98. ** can give extra help or supply local customizations by merely editing
  99. ** the help file.
  100. **
  101. ** The original (single-file) idea and algorithm is by John D. Johnson,
  102. ** Hewlett-Packard Company.  Thanx and a tip of the Hatlo hat!
  103. **
  104. ** Much extension by David Kotz for use in gnutex, and then in gnuplot.
  105. ** Added output paging support, both unix and builtin. Rewrote completely
  106. ** to read helpfile into memory, avoiding reread of help file. 12/89.
  107. **
  108. ** Modified by Russell Lang to avoid reading completely into memory
  109. ** if MSDOS defined.  This uses much less memory.  6/91
  110. **
  111. ** The help file looks like this (the question marks are really in column 1):
  112. **
  113. **     ?topic
  114. **     This line is printed when the user wants help on "topic".
  115. **     ?keyword
  116. **     ?Keyword
  117. **     ?KEYWORD
  118. **     These lines will be printed on the screen if the user wanted
  119. **     help on "keyword", "Keyword", or "KEYWORD".  No casefolding is
  120. **    done on the keywords.
  121. **     ?subject
  122. **     ?alias
  123. **     This line is printed for help on "subject" and "alias".
  124. **     ?
  125. **    ??
  126. **     Since there is a null keyword for this line, this section
  127. **     is printed when the user wants general help (when a help
  128. **     keyword isn't given).  A command summary is usually here.
  129. **    Notice that the null keyword is equivalent to a "?" keyword
  130. **    here, because of the '?' and '??' topic lines above.
  131. **   If multiple keywords are given, the first is considered the 
  132. **   'primary' keyword. This affects a listing of available topics.
  133. **     ?last-subject
  134. **     Note that help sections are terminated by the start of the next
  135. **     '?' entry or by EOF.  So you can't have a leading '?' on a line
  136. **     of any help section.  You can re-define the magic character to
  137. **    recognize in column 1, though, if '?' is too useful.  (Try ^A.)
  138. */
  139.  
  140. #define    KEYFLAG    '?'    /* leading char in help file topic lines */
  141.  
  142. /*
  143. ** Calling sequence:
  144. **    int result;        # 0 == success
  145. **    char *keyword;        # topic to give help on
  146. **    char *pathname;        # path of help file
  147. **      int subtopics;        # set to TRUE if only subtopics to be listed
  148. **                # returns TRUE if subtopics were found
  149. **    result = help(keyword, pathname, &subtopics);
  150. ** Sample:
  151. **    cmd = "search\n";
  152. **    helpfile = "/usr/local/lib/program/program.help";
  153. **    subtopics = FALSE;
  154. **    if (help(cmd, helpfile, &subtopics) != H_FOUND)
  155. **        printf("Sorry, no help for %s", cmd);
  156. **
  157. **
  158. ** Speed this up by replacing the stdio calls with open/close/read/write.
  159. */
  160. #ifdef    WDLEN
  161. #  define    PATHSIZE    WDLEN
  162. #else
  163. #  define    PATHSIZE    BUFSIZ
  164. #endif
  165.  
  166. typedef int boolean;
  167. #ifndef TRUE
  168. #define TRUE (1)
  169. #define FALSE (0)
  170. #endif
  171.  
  172. typedef struct line_s LINEBUF;
  173. struct line_s {
  174.     char *line;            /* the text of this line */
  175.     LINEBUF *next;            /* the next line */
  176. };
  177.  
  178. typedef struct linkey_s LINKEY;
  179. struct linkey_s {
  180.     char *key;                /* the name of this key */
  181.     long pos;                /* ftell position */
  182.     LINEBUF *text;            /* the text for this key */
  183.     boolean primary;        /* TRUE -> is a primary name for a text block */
  184.     LINKEY *next;            /* the next key in linked list */
  185. };
  186.  
  187. typedef struct key_s KEY;
  188. struct key_s {
  189.     char *key;                /* the name of this key */
  190.     long pos;                /* ftell position */
  191.     LINEBUF *text;            /* the text for this key */
  192.     boolean primary;        /* TRUE -> is a primary name for a text block */
  193. };
  194. static LINKEY *keylist = NULL;    /* linked list of keys */
  195. static KEY *keys = NULL;        /* array of keys */
  196. static int keycount = 0;        /* number of keys */
  197. static FILE *helpfp = NULL;
  198.  
  199. static int LoadHelp();
  200. static void sortkeys();
  201. static int keycomp();
  202. static LINEBUF *storeline();
  203. static LINKEY *storekey();
  204. static KEY *FindHelp();
  205. static boolean Ambiguous();
  206.  
  207. /* Help output */
  208. static void PrintHelp();
  209. static void ShowSubtopics();
  210. static void StartOutput();
  211. static void OutLine();
  212. static void EndOutput();
  213. static FILE *outfile;        /* for unix pager, if any */
  214. static int pagelines;        /* count for builtin pager */
  215. #define SCREENSIZE 24        /* lines on screen (most have at least 24) */
  216.  
  217. /* help:
  218.  * print a help message 
  219.  * also print available subtopics, if subtopics is TRUE
  220.  */
  221. help(keyword, path, subtopics)
  222.     char *keyword;        /* on this topic */
  223.     char *path;            /* from this file */
  224.     boolean *subtopics;    /* (in) - subtopics only? */
  225.                         /* (out) - are there subtopics? */
  226. {
  227.     static char oldpath[PATHSIZE] = "";    /* previous help file */
  228.     int status;            /* result of LoadHelp */
  229.     KEY *key;            /* key that matches keyword */
  230.  
  231.     /*
  232.     ** Load the help file if necessary (say, first time we enter this routine,
  233.     ** or if the help file changes from the last time we were called).
  234.     ** Also may occur if in-memory copy was freed.
  235.     ** Calling routine may access errno to determine cause of H_ERROR.
  236.     */
  237.     errno = 0;
  238.     if (strncmp(oldpath, path, PATHSIZE) != SAME)
  239.      FreeHelp();
  240.     if (keys == NULL) {
  241.        status = LoadHelp(path);
  242.        if (status == H_ERROR)
  243.         return(status);
  244.  
  245.        /* save the new path in oldpath */
  246.        if (strlen(path) < PATHSIZE)
  247.         (void) strcpy(oldpath, path);
  248.        else {                /* not enough room in oldpath, sigh */
  249.           (void) strncpy(oldpath, path, PATHSIZE - 1);
  250.           oldpath[PATHSIZE - 1] = '\0';
  251.        }
  252.     }
  253.  
  254.     /* look for the keyword in the help file */
  255.     key = FindHelp(keyword);
  256.     if (key != NULL) {
  257.        /* found the keyword: print help and return */
  258.        PrintHelp(key, subtopics);
  259.        status = H_FOUND;
  260.     } else {
  261.        status = H_NOTFOUND;
  262.     }
  263.  
  264.     return(status);
  265. }
  266.  
  267. /* we only read the file once, into memory
  268.  * except for MSDOS when we don't read all the file -
  269.  * just the keys and location of the text
  270.  */
  271. static int
  272. LoadHelp(path)
  273.     char *path;
  274. {
  275.     LINKEY *key;            /* this key */
  276.     long pos;                /* ftell location within help file */
  277.     char buf[BUFSIZ];        /* line from help file */
  278.     LINEBUF *head;            /* head of text list  */
  279.     LINEBUF *firsthead = NULL;
  280.     boolean primary;        /* first ? line of a set is primary */
  281.     boolean flag;
  282.  
  283.     if ((helpfp = fopen(path, "r")) == NULL) {
  284.        /* can't open help file, so error exit */
  285.        return (H_ERROR);
  286.     }
  287.  
  288.     /*
  289.     ** The help file is open.  Look in there for the keyword.
  290.     */
  291.     (void) fgets(buf, BUFSIZ - 1, helpfp);
  292.     while (!feof(helpfp)) {
  293.        /*
  294.         ** Make an entry for each synonym keyword
  295.         */
  296.        primary = TRUE;
  297.        while (buf[0] == KEYFLAG) {
  298.           key = storekey(buf+1);    /* store this key */
  299.           key->primary = primary;
  300.           key->text = NULL;            /* fill in with real value later */
  301.           key->pos = 0;                /* fill in with real value later */
  302.           primary = FALSE;
  303.           pos = ftell(helpfp);
  304.           if (fgets(buf, BUFSIZ - 1, helpfp) == (char *)NULL)
  305.             break;
  306.        }
  307.        /*
  308.         ** Now store the text for this entry.
  309.         ** buf already contains the first line of text.
  310.         */
  311. #ifndef MSDOS
  312.        firsthead = storeline(buf);
  313.        head = firsthead;
  314. #endif
  315.        while ( (fgets(buf, BUFSIZ - 1, helpfp) != (char *)NULL)
  316.         && (buf[0] != KEYFLAG) ){
  317. #ifndef MSDOS
  318.           /* save text line */
  319.           head->next = storeline(buf);
  320.           head = head->next;
  321. #endif
  322.        }
  323.        /* make each synonym key point to the same text */
  324.        do {
  325.           key->pos = pos;
  326.           key->text = firsthead;
  327.           flag = key->primary;
  328.           key = key->next;
  329.        } while ( flag!=TRUE  &&  key!=NULL );
  330.     }
  331. #ifndef MSDOS
  332.     (void) fclose(helpfp);
  333. #endif
  334.  
  335.     /* we sort the keys so we can use binary search later */
  336.     sortkeys();
  337.     return(H_FOUND); /* ok */
  338. }
  339.  
  340. /* make a new line buffer and save this string there */
  341. static LINEBUF *
  342. storeline(text)
  343.     char *text;
  344. {
  345.     LINEBUF *new;
  346.  
  347.     new = (LINEBUF *)malloc(sizeof(LINEBUF));
  348.     if (new == NULL)
  349.      int_error("not enough memory to store help file", -1);
  350.     if (text != NULL) {
  351.        new->line = (char *) malloc((unsigned int)(strlen(text)+1));
  352.        if (new->line == NULL)
  353.         int_error("not enough memory to store help file", -1);
  354.        (void) strcpy(new->line, text);
  355.     } else
  356.      new->line = NULL;
  357.  
  358.     new->next = NULL;
  359.  
  360.     return(new);
  361. }
  362.  
  363. /* Add this keyword to the keys list, with the given text */
  364. static LINKEY *
  365. storekey(key)
  366.     char *key;
  367. {
  368.     LINKEY *new;
  369.  
  370.     key[strlen(key)-1] = '\0'; /* cut off \n  */
  371.  
  372.     new = (LINKEY *)malloc(sizeof(LINKEY));
  373.     if (new == NULL)
  374.      int_error("not enough memory to store help file", -1);
  375.     new->key = (char *) malloc((unsigned int)(strlen(key)+1));
  376.     if (new->key == NULL)
  377.      int_error("not enough memory to store help file", -1);
  378.     (void) strcpy(new->key, key);
  379.  
  380.     /* add to front of list */
  381.     new->next = keylist;
  382.     keylist = new;
  383.     keycount++;
  384.     return(new);
  385. }
  386.  
  387. /* we sort the keys so we can use binary search later */
  388. /* We have a linked list of keys and the number.
  389.  * to sort them we need an array, so we reform them into an array,
  390.  * and then throw away the list.
  391.  */
  392. static void
  393. sortkeys()
  394. {
  395.     LINKEY *p,*n;            /* pointers to linked list */
  396.     int i;                /* index into key array */
  397.     
  398.     /* allocate the array */
  399.     keys = (KEY *)malloc((unsigned int)((keycount+1) * sizeof(KEY)));
  400.     if (keys == NULL)
  401.      int_error("not enough memory to store help file", -1);
  402.     
  403.     /* copy info from list to array, freeing list */
  404.     for (p = keylist, i = 0; p != NULL; p = n, i++) {
  405.        keys[i].key = p->key;
  406.        keys[i].pos = p->pos;
  407.        keys[i].text = p->text;
  408.        keys[i].primary = p->primary;
  409.        n = p->next;
  410.        free( (char *)p );
  411.     }
  412.  
  413.     /* a null entry to terminate subtopic searches */
  414.     keys[keycount].key = NULL;
  415.     keys[keycount].pos = 0;
  416.     keys[keycount].text = NULL;
  417.  
  418.     /* sort the array */
  419.     /* note that it only moves objects of size (two pointers + long + int) */
  420.     /* it moves no strings */
  421.     qsort((char *)keys, keycount, sizeof(KEY), keycomp);
  422. }
  423.  
  424. static int
  425. keycomp(a, b)
  426.     KEY *a,*b;
  427. {
  428.     return (strcmp(a->key, b->key));
  429. }
  430.  
  431. /* Free the help file from memory. */
  432. /* May be called externally if space is needed */
  433. void
  434. FreeHelp()
  435. {
  436.     int i;                /* index into keys[] */
  437.     LINEBUF *t, *next;
  438.  
  439.     if (keys == NULL)
  440.      return;
  441.  
  442.     for (i = 0; i < keycount; i++) {
  443.        free( (char *)keys[i].key );
  444.        if (keys[i].primary)   /* only try to release text once! */
  445.        for (t = keys[i].text; t != NULL; t = next) {
  446.           free( (char *)t->line );
  447.           next = t->next;
  448.           free( (char *)t );
  449.        }
  450.     }
  451.     free( (char *)keys );
  452.     keys = NULL;
  453.     keycount = 0;
  454. #ifdef MSDOS
  455.     (void) fclose(helpfp);
  456. #endif
  457. }
  458.  
  459. /* FindHelp:
  460.  *  Find the key that matches the keyword.
  461.  *  The keys[] array is sorted by key.
  462.  *  We could use a binary search, but a linear search will aid our
  463.  *  attempt to allow abbreviations. We search for the first thing that
  464.  *  matches all the text we're given. If not an exact match, then
  465.  *  it is an abbreviated match, and there must be no other abbreviated
  466.  *  matches -- for if there are, the abbreviation is ambiguous. 
  467.  *  We print the ambiguous matches in that case, and return not found.
  468.  */
  469. static KEY *                /* NULL if not found */
  470. FindHelp(keyword)
  471.     char *keyword;            /* string we look for */
  472. {
  473.     KEY *key;
  474.     int len = strlen(keyword);
  475.     int compare;
  476.  
  477.     for (key = keys, compare = 1; key->key != NULL && compare > 0; key++) {
  478.        compare = strncmp(keyword, key->key, len);
  479.        if (compare == 0)    /* we have a match! */
  480.         if (!Ambiguous(key, len)) {
  481.             /* non-ambiguous abbreviation */
  482.             (void) strcpy(keyword, key->key); /* give back the full spelling */
  483.             return(key);        /* found!! */
  484.         }
  485.     }
  486.  
  487.     /* not found, or ambiguous */
  488.     return(NULL);
  489. }
  490.  
  491. /* Ambiguous:
  492.  * Check the key for ambiguity up to the given length.
  493.  * It is ambiguous if it is not a complete string and there are other
  494.  * keys following it with the same leading substring.
  495.  */
  496. static boolean
  497. Ambiguous(key, len)
  498.     KEY *key;
  499.     int len;
  500. {
  501.     char *first;
  502.     char *prev;
  503.     boolean status = FALSE;    /* assume not ambiguous */
  504.     int compare;
  505.     int sublen;
  506.  
  507.     if (key->key[len] == '\0')
  508.      return(FALSE);
  509.     
  510.     for (prev = first = key->key, compare = 0, key++;
  511.         key->key != NULL && compare == 0; key++) {
  512.        compare = strncmp(first, key->key, len);
  513.        if (compare == 0) {
  514.           /* So this key matches the first one, up to len.
  515.            * But is it different enough from the previous one
  516.            * to bother printing it as a separate choice?
  517.            */
  518.           sublen = instring(prev+len, ' ');
  519.           if (strncmp(key->key, prev, len+sublen) != 0) {
  520.              /* yup, this is different up to the next space */
  521.              if (!status) {
  522.                 /* first one we have printed is special */
  523.                 fprintf(stderr, 
  524.                        "Ambiguous request '%.*s'; possible matches:\n",
  525.                        len, first);
  526.                 fprintf(stderr, "\t%s\n", prev);
  527.                 status = TRUE;
  528.              }
  529.              fprintf(stderr, "\t%s\n", key->key);
  530.              prev = key->key;
  531.           }
  532.        }
  533.     }
  534.     
  535.     return(status);
  536. }
  537.  
  538. /* PrintHelp:
  539.  * print the text for key
  540.  */
  541. static void
  542. PrintHelp(key, subtopics)
  543.     KEY *key;
  544.     boolean *subtopics;        /* (in) - subtopics only? */
  545.                         /* (out) - are there subtopics? */
  546. {
  547.     LINEBUF *t;
  548. #ifdef MSDOS
  549.     char buf[BUFSIZ];        /* line from help file */
  550. #endif
  551.  
  552.     StartOutput();
  553.  
  554.     if (subtopics == NULL || !*subtopics) {
  555. #ifdef MSDOS
  556.        fseek(helpfp,key->pos,0);
  557.        while ( (fgets(buf, BUFSIZ - 1, helpfp) != (char *)NULL)
  558.             && (buf[0] != KEYFLAG) ) {
  559.           OutLine(buf);
  560.        }
  561. #else
  562.        for (t = key->text; t != NULL; t = t->next)
  563.         OutLine(t->line);        /* print text line */
  564. #endif
  565.     }
  566.  
  567.     ShowSubtopics(key, subtopics);
  568.     OutLine("\n");
  569.  
  570.     EndOutput();
  571. }
  572.  
  573. /* ShowSubtopics:
  574.  *  Print a list of subtopic names
  575.  */
  576. #define PER_LINE 4
  577.  
  578. static void
  579. ShowSubtopics(key, subtopics)
  580.     KEY *key;                /* the topic */
  581.     boolean *subtopics;        /* (out) are there any subtopics */
  582. {
  583.     int subt = 0;            /* printed any subtopics yet? */
  584.     KEY *subkey;            /* subtopic key */
  585.     int len;                /* length of key name */
  586.     char line[BUFSIZ];        /* subtopic output line */
  587.     char *start;            /* position of subname in key name */
  588.     int sublen;            /* length of subname */
  589.     int pos;
  590.     char *prev = NULL;        /* the last thing we put on the list */
  591.  
  592.     *line = '\0';
  593.     len = strlen(key->key);
  594.  
  595.     for (subkey = key+1; subkey->key != NULL; subkey++) {
  596.        if (strncmp(subkey->key, key->key, len) == 0) {
  597.           /* find this subtopic name */
  598.           start = subkey->key + len;
  599.           if (len > 0)
  600.             if (*start == ' ')
  601.              start++;        /* skip space */
  602.             else
  603.              break;        /* not the same topic after all  */
  604.           else            /* here we are looking for main topics */
  605.             if (!subkey->primary)
  606.              continue;    /* not a main topic */
  607.           sublen = instring(start, ' ');
  608.           if (prev == NULL || strncmp(start, prev, sublen) != 0) {
  609.              if (subt == 0) {
  610.                 subt++;
  611.                 if (len)
  612.                   (void) sprintf(line, "\nSubtopics available for %s:\n", 
  613.                         key->key);
  614.                 else
  615.                   (void) sprintf(line, "\nHelp topics available:\n");
  616.                 OutLine(line);
  617.                 *line = '\0';
  618.                 pos = 0;
  619.              }
  620.              if (pos == PER_LINE) {
  621.                 (void) strcat(line, "\n");
  622.                 OutLine(line);
  623.                 *line = '\0';
  624.                 pos = 0;
  625.              }
  626.              /* adapted by DvdSchaaf */
  627.              {
  628. #define FIRSTCOL    6
  629. #define COLLENGTH    15
  630.                 int spacelen, ispacelen;
  631.  
  632.                  if( pos == 0 )
  633.                     spacelen = FIRSTCOL;
  634.                  for( ispacelen = 0;
  635.                     ispacelen < spacelen; ispacelen++ )
  636.                     (void) strcat(line, " ");
  637.                  /* commented out *
  638.                  (void) strcat(line, "\t");
  639.                  */
  640.                  (void) strncat(line, start, sublen);
  641.                  spacelen = COLLENGTH - sublen;
  642.                  if( spacelen <= 0 )
  643.                     spacelen = 1;
  644.              }
  645.              pos++;
  646.              prev = start;
  647.           }
  648.        } else {
  649.           /* new topic */
  650.           break;
  651.        }
  652.     }
  653.     
  654.     /* put out the last line */
  655.     if (subt > 0 && pos > 0) {
  656.        (void) strcat(line, "\n");
  657.        OutLine(line);
  658.     }
  659.     
  660. /*
  661.     if (subt == 0) {
  662.        OutLine("\n");
  663.        OutLine("No subtopics available\n");
  664.     }
  665. */
  666.     
  667.     if (subtopics)
  668.      *subtopics = (subt != 0);
  669. }
  670.  
  671.  
  672. /* StartOutput:
  673.  * Open a file pointer to a pipe to user's $PAGER, if there is one,
  674.  * otherwise use our own pager.
  675.  */
  676. static void
  677. StartOutput()
  678. {
  679. #ifdef unix
  680.     char *pager_name = getenv("PAGER");
  681.     extern FILE *popen();
  682.  
  683.     if (pager_name != NULL && *pager_name != '\0')
  684.      if ((outfile = popen(pager_name, "w")) != (FILE *)NULL)
  685.        return;            /* success */
  686.     outfile = stderr;
  687.     /* fall through to built-in pager */
  688. #endif
  689.  
  690.     /* built-in pager */
  691.     pagelines = 0;
  692. }
  693.  
  694. /* write a line of help output  */
  695. /* line should contain only one \n, at the end */
  696. static void
  697. OutLine(line)
  698.     char *line;
  699. {
  700.     int c;                /* dummy input char */
  701. #ifdef unix
  702.     if (outfile != stderr) {
  703.        fputs(line, outfile);
  704.        return;
  705.     }
  706. #endif
  707.  
  708.     /* built-in dumb pager */
  709.     /* leave room for prompt line */
  710.     if (pagelines >= SCREENSIZE - 2) {
  711.        fprintf(stderr,"Press return for more: ");
  712.        do 
  713.         c = getchar();
  714.        while (c != EOF && c != '\n');
  715.        pagelines = 0;
  716.     }
  717.     fputs(line, stderr);
  718.     pagelines++;
  719. }
  720.  
  721. static void
  722. EndOutput()
  723. {
  724. #ifdef unix
  725.     extern int pclose();
  726.  
  727.     if (outfile != stderr)
  728.      (void) pclose(outfile);
  729. #endif
  730. }
  731.